import { $uniPromisify } from '../uniPromisify'
import i18n from '@/locale'

export default {
  /**
   * @param {String} title
	 * @param {Object} options
   * 提示框
   */
  toast(title, options = { icon: 'none', position: 'center', duration: 2000 }) {
    if (Boolean(title) === false) {
      return
    }
    return uni.showToast({
      title,
      ...options
    })
  },
  /**
   * @param {String} content
   * @param {String} title
   * @param {Object} options
   * 提示框
   */
  alert(content, title = i18n.t('public.t1'), options = { showCancel: false, }) {
    return uni.showModal({
      content,
      title,
      ...options
    })
  },
  /**
   * @param {String} title
   * @param {String} content
   * @param {Object} options
   * 确认提示框
   */
  confirm(title = i18n.t('public.t2'), content = '', options = {}) {
    return new Promise(async (resolve, reject) => {
      const data = await $uniPromisify(uni.showModal)({
			  title,
			  content,
			  cancelText:i18n.t('public.t10'),
			  confirmText:i18n.t('public.t9'),
			  ...options
      })
      if (data.confirm) {
			  resolve()
      } else {
			  reject('取消操作')
      }
    })
  },
  /**
   * @param {String} title
   * @param {Boolean} mask
   * loading加载框
   */
  loading(title = i18n.t('public.t3'), mask = true) {
    return uni.showLoading({
      title,
      mask
    })
  },
  /**
   * 隐藏loading加载框
   */
  hideLoading() {
    uni.hideLoading()
  },
  /**
   * @param {Array} itemList
   * 从底部向上弹出操作菜单
   */
  actionSheet(itemList = []) {
    return new Promise((resolve, reject) => {
      uni.showActionSheet({
        itemList,
        success: res => {
          resolve(res.tapIndex)
        },
        fail: () => {
          reject('取消操作')
        }
      })
    })
  }
}
